package com.wjh.config;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.RedeliveryPolicy;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.listener.DefaultMessageListenerContainer;

import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.Session;

@Configuration
@EnableJms
public class ActivemqConfig {

    @Value("${spring.online.activemq.brokerUrl}")
    private String onlineBrokerUrl;
    @Value("${spring.online.activemq.user}")
    private String onlineUsername;
    @Value("${spring.online.activemq.password}")
    private String onlinePassword;

    @Value("${spring.offline.activemq.brokerUrl}")
    private String offlineBrokerUrl;
    @Value("${spring.offline.activemq.user}")
    private String offlineUsername;
    @Value("${spring.offline.activemq.password}")
    private String offlinePassword;

    @Bean
    public RedeliveryPolicy redeliveryPolicy(){
        RedeliveryPolicy redeliveryPolicy= new RedeliveryPolicy();
        //是否在每次尝试重新发送失败后,增长这个等待时间
        redeliveryPolicy.setUseExponentialBackOff(true);
        //重发次数,默认为6次   这里设置为10次
        redeliveryPolicy.setMaximumRedeliveries(10);
        //重发时间间隔,默认为1秒
        redeliveryPolicy.setInitialRedeliveryDelay(30);
        //第一次失败后重新发送之前等待500毫秒,第二次失败再等待500 * 2毫秒,这里的2就是value
        redeliveryPolicy.setBackOffMultiplier(60);
        //是否避免消息碰撞
        redeliveryPolicy.setUseCollisionAvoidance(false);
        //设置重发最大拖延时间-1 表示没有拖延只有UseExponentialBackOff(true)为true时生效
        redeliveryPolicy.setMaximumRedeliveryDelay(-1);
        return redeliveryPolicy;
    }



//    @Bean(value="onlineActiveMQConnectionFactory")
//    public ActiveMQConnectionFactory onlineActiveMQConnectionFactory (RedeliveryPolicy redeliveryPolicy){
//        ActiveMQConnectionFactory activeMQConnectionFactory =
//                new ActiveMQConnectionFactory(onlineUsername,onlinePassword,onlineBrokerUrl);
//        activeMQConnectionFactory.setRedeliveryPolicy(redeliveryPolicy);
//        return activeMQConnectionFactory;
//    }
    @Bean(value="offlineActiveMQConnectionFactory")
    public ActiveMQConnectionFactory offlineActiveMQConnectionFactory (RedeliveryPolicy redeliveryPolicy){
        ActiveMQConnectionFactory activeMQConnectionFactory =
                new ActiveMQConnectionFactory(offlineUsername,offlinePassword,offlineBrokerUrl);
        activeMQConnectionFactory.setRedeliveryPolicy(redeliveryPolicy);
        return activeMQConnectionFactory;
    }





//    @Primary
//    @Bean
//    public JmsTemplate onlineJmsTemplate(ActiveMQConnectionFactory onlineActiveMQConnectionFactory){
//        JmsTemplate jmsTemplate=new JmsTemplate();
//        //进行持久化配置 1表示非持久化，2表示持久化
//        jmsTemplate.setDeliveryMode(DeliveryMode.PERSISTENT);
//        jmsTemplate.setConnectionFactory(onlineActiveMQConnectionFactory);
//        //此处可不设置默认，在发送消息时也可设置队列
////        jmsTemplate.setDefaultDestination(queue);
//        //客户端签收模式
//        jmsTemplate.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
//        return jmsTemplate;
//    }

    @Bean
    public JmsTemplate offlineJmsTemplate(ActiveMQConnectionFactory offlineActiveMQConnectionFactory){
        JmsTemplate jmsTemplate=new JmsTemplate();
        //进行持久化配置 1表示非持久化，2表示持久化
        jmsTemplate.setDeliveryMode(DeliveryMode.PERSISTENT);
        jmsTemplate.setConnectionFactory(offlineActiveMQConnectionFactory);
        //此处可不设置默认，在发送消息时也可设置队列
//        jmsTemplate.setDefaultDestination(queue);
        //客户端签收模式
        jmsTemplate.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
        return jmsTemplate;
    }

    /**
     * 定义一个消息监听器连接工厂，这里定义的是点对点模式的监听器连接工厂
     * @param onlineActiveMQConnectionFactory
     * @return
     */
//    @Bean(value = "onlineJmsQueueListener")
//    public DefaultJmsListenerContainerFactory onlineJmsQueueListenerContainerFactory(ActiveMQConnectionFactory onlineActiveMQConnectionFactory) {
//        DefaultJmsListenerContainerFactory factory =
//                new DefaultJmsListenerContainerFactory();
//        factory.setConnectionFactory(onlineActiveMQConnectionFactory);
//        //设置连接数
//        factory.setConcurrency("1-10");
//        //重连间隔时间
//        factory.setRecoveryInterval(1000L);
//        factory.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
//        return factory;
//    }

    /**
     * 定义一个消息监听器连接工厂，这里定义的是点对点模式的监听器连接工厂
     * @param offlineActiveMQConnectionFactory
     * @return
     */
    @Bean(value = "offlineJmsQueueListener")
    public DefaultJmsListenerContainerFactory offlineJmsQueueListenerContainerFactory(ActiveMQConnectionFactory offlineActiveMQConnectionFactory) {
        DefaultJmsListenerContainerFactory factory =
                new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(offlineActiveMQConnectionFactory);
        //设置连接数
        factory.setConcurrency("1-10");
        //重连间隔时间
        factory.setRecoveryInterval(1000L);
        factory.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
        return factory;
    }
}
